package com.ruoyi.web.controller.borehole;

import cn.hutool.http.HttpStatus;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.web.domain.BhRiskList;
import com.ruoyi.web.domain.BhRiskPost;
import com.ruoyi.web.domain.query.AddRiskListQuery;
import com.ruoyi.web.domain.query.RiskPageQuery;
import com.ruoyi.web.domain.vo.BhRiskListPageVO;
import com.ruoyi.web.domain.vo.ReportActivityVO;
import com.ruoyi.web.service.BhPostCheckService;
import com.ruoyi.web.service.BhRiskListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Tag(name = "风险库")
@RestController
@RequestMapping("/riskList")
public class BhRiskListController extends BaseController {

    @Autowired
    private BhRiskListService bhRiskListService;

    @Operation(summary = "获取全量数据")
    @GetMapping("/getAllData")
    public R<List<ReportActivityVO>> getAllData(@RequestParam("tabName") String tabName) {
        return bhRiskListService.getAllData(tabName);
    }

    @Operation(summary = "风险库详情")
    @GetMapping("/getRiskList")
    public R<BhRiskList> getReportRiskList(@RequestParam("id") Integer id) {
        return bhRiskListService.getReportRiskList(id);
    }

    @Operation(summary = "风险库列表页 - 分页查询")
    @GetMapping("/getRiskPage")
    public TableDataInfo<BhRiskListPageVO> getRiskPage(@Validated(QueryGroup.class) RiskPageQuery riskPageQuery) {
        TableDataInfo<BhRiskListPageVO> result = new TableDataInfo<>();
        if (Objects.isNull(riskPageQuery)) {
            result.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
            result.setMsg("参数错误");
            return result;
        }
        if (StringUtils.isBlank(riskPageQuery.getTabName())) {
            result.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
            result.setMsg("模板类型为空");
            return result;
        }
        return bhRiskListService.getRiskListPage(riskPageQuery);
    }

    @Operation(summary = "新增风险库")
    @RepeatSubmit
    @PostMapping("/addRiskList")
    public R<Void> addRiskList(@RequestBody AddRiskListQuery addRiskListQuery) {
        if (StringUtils.isBlank(addRiskListQuery.getTabName())) {
            return R.fail("缺少模板类型");
        }
        if (StringUtils.isBlank(addRiskListQuery.getMainName())) {
            return R.fail("缺少主单元");
        }
        addRiskListQuery.setCreateTime(new Date());
        addRiskListQuery.setCreatedBy(getUsername());
        return bhRiskListService.addRiskList(addRiskListQuery);
    }

    @Operation(summary = "编辑风险库")
    @RepeatSubmit
    @PostMapping("/modRiskList")
    public R<Void> modRiskList(@RequestBody AddRiskListQuery addRiskListQuery) {
        if (StringUtils.isBlank(addRiskListQuery.getTabName())) {
            return R.fail("缺少模板类型");
        }
        addRiskListQuery.setUpdatedTime(new Date());
        addRiskListQuery.setUpdatedBy(getUsername());
        return bhRiskListService.modRiskList(addRiskListQuery);
    }

    @Operation(summary = "导入风险库数据")
    @RepeatSubmit
    @PostMapping("/importRiskList")
    public R<Void> importExcel(@RequestParam("file") MultipartFile file, @RequestParam("tabName") String tabName) {
        try {
            return bhRiskListService.importExcel(file.getInputStream(), tabName);
        } catch (Exception e) {
            return R.fail("文件导入失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除风险库")
    @RepeatSubmit
    @PostMapping("/delRiskById")
    public R<Void> delRiskById(@RequestBody AddRiskListQuery query) {
        if (query.getId() == null) {
            return R.fail("缺少删除对象");
        }
        return bhRiskListService.delRiskById(query);
    }

    @Autowired
    private BhPostCheckService bhPostCheckService;

    @Operation(summary = "风险库 - 获取现场检查项目")
    @GetMapping("/getAllRiskPost")
    public R<List<BhRiskPost>> getAllRiskPost() {
        return bhPostCheckService.getAllRiskPost();
    }
}
